package com.ruoyi.entity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.entity.domain.Recommend;
import com.ruoyi.entity.service.IRecommendService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 推荐Controller
 * 
 * @author Delamer
 * @date 2020-12-30
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/entity/recommend" )
public class RecommendController extends BaseController {

    private final IRecommendService recommendService;

    /**
     * 查询推荐列表
     */
    @PreAuthorize("@ss.hasPermi('entity:recommend:list')")
    @GetMapping("/list")
    public TableDataInfo list(Recommend recommend)
    {
        startPage();
        LambdaQueryWrapper<Recommend> lqw = new LambdaQueryWrapper<Recommend>();
        if (StringUtils.isNotBlank(recommend.getRecommendName())){
            lqw.like(Recommend::getRecommendName ,recommend.getRecommendName());
        }
        if (StringUtils.isNotBlank(recommend.getRecommendPath())){
            lqw.eq(Recommend::getRecommendPath ,recommend.getRecommendPath());
        }
        if (recommend.getRecommendStatus() != null){
            lqw.eq(Recommend::getRecommendStatus ,recommend.getRecommendStatus());
        }
        List<Recommend> list = recommendService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出推荐列表
     */
    @PreAuthorize("@ss.hasPermi('entity:recommend:export')" )
    @Log(title = "推荐" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Recommend recommend) {
        LambdaQueryWrapper<Recommend> lqw = new LambdaQueryWrapper<Recommend>(recommend);
        List<Recommend> list = recommendService.list(lqw);
        ExcelUtil<Recommend> util = new ExcelUtil<Recommend>(Recommend. class);
        return util.exportExcel(list, "recommend" );
    }

    /**
     * 获取推荐详细信息
     */
    @PreAuthorize("@ss.hasPermi('entity:recommend:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(recommendService.getById(id));
    }

    /**
     * 新增推荐
     */
    @PreAuthorize("@ss.hasPermi('entity:recommend:add')" )
    @Log(title = "推荐" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Recommend recommend) {
        return toAjax(recommendService.save(recommend) ? 1 : 0);
    }

    /**
     * 修改推荐
     */
    @PreAuthorize("@ss.hasPermi('entity:recommend:edit')" )
    @Log(title = "推荐" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Recommend recommend) {
        return toAjax(recommendService.updateById(recommend) ? 1 : 0);
    }

    /**
     * 删除推荐
     */
    @PreAuthorize("@ss.hasPermi('entity:recommend:remove')" )
    @Log(title = "推荐" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(recommendService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
